9 namespace Core.TypeCast
12 using System.Reflection;
14 using System.Runtime.CompilerServices;
20 public static partial class ObjectExtension
54 [MethodImpl(MethodImplOptions.AggressiveInlining)]
55 public static TOut ConvertTo<TIn, TOut>(
this TIn
self,
object model,
bool withContext =
false)
57 TOut result =
default(TOut);
58 self.TryConvert<TOut>(out result, model, throwException: model != null, withContext: withContext);
92 [MethodImpl(MethodImplOptions.AggressiveInlining)]
93 public static TOut ConvertTo<TOut>(
this object self,
object model,
bool withContext =
false)
95 TOut result =
default(TOut);
96 self.TryConvert<TOut>(out result, model, throwException: model != null, withContext: withContext);
124 public static bool TryConvert<TOut>(
this object self, out TOut result,
object model,
bool throwException =
false,
bool withContext =
false)
127 result =
default(TOut);
128 if(GetConverterOrDefault<object, TOut>(
self, out converter, out result, typeArgument: model?.GetType(), throwException: throwException, unboxObjectType:
true, withContext: withContext))
133 return InvokeConvert(
self, out result, model, throwException, converter, contextInstance: (withContext ?
new ConvertContext(model) : null));
161 public static bool TryConvert<TIn, TOut, TArg>(
this TIn
self, out TOut result, TArg model,
bool throwException =
false,
bool withContext =
false)
164 result =
default(TOut);
165 if(GetConverterOrDefault<TIn, TOut>(
self, out converter, out result, typeArgument: typeof(TArg), throwException: throwException, unboxObjectType:
false, withContext: withContext))
170 return InvokeConvert(
self, out result, model, throwException, converter, contextInstance: (withContext ?
new ConvertContext(model) : null));